<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 layout布局 -->
        <tree-tools :node-data="company" :is-root="true" @add-depts="handleAddDepts" />
        <el-tree
          :data="departs"
          :props="defaultProps"
          default-expand-all
        >
          <!-- 主要用的是data字段，node字段仅做了解 双大括号语法会报错
        data 当前节点的数据-->
          <template #default="{data}">
            <!-- {{ data.name }}{{ data.manager }} -->
            <!-- <tree-tools :node-data="data" /> -->
            <!-- 非头部 一级部门二级部门... -->
            <!-- isRoot父传子 默认值就是false 所以可以不写 -->
            <!-- <tree-tools :node-data="data" :is-root="false" /> -->
            <tree-tools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- @update:dialogVisible="close" -->
    <!-- @update:dialogVisible="dialogVisible=$event" -->
    <add-depts
      ref="addDeptRef"
      :dialog-visible.sync="dialogVisible"
      :node-data="nodeData"
      :depts="depts"
      @addArray="getDepartments"
    />
  </div>
</template>

<script>
import { reqGetDepartments } from '@/api/departments'
import treeTools from './components/tree-tools.vue'
import { tranListToTreeData } from '@/utils/index'
import AddDepts from './components/add-depts.vue'
export default {
  // inject: ['foo'],
  name: 'Departments',
  components: { treeTools, AddDepts },
  data() {
    return {
      loading: false,
      depts: [],
      nodeData: {},
      dialogVisible: false,
      company: {},
      departs: [
        {
          name: '总裁办',
          manager: '曹操',
          children: [{ name: '董事会', manager: '曹丕' }]
        },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  // 因为增删改查所以放到methods里多次调用
  async created() {
    // const { data: { depts }} = await reqGetDepartments()
    // // console.log(depts)
    // this.departs = depts
    this.getDepartments()
    // console.log(this.foo)
  },

  methods: {
    // close(flag) {
    //   this.dialogVisible = flag
    // },
    async getDepartments() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetDepartments()
      // console.log(JSON.parse(JSON.stringify(depts)))
      this.company = { name: companyName, manager: '负责人', id: '' }
      // this.departs = depts
      this.departs = tranListToTreeData(depts, '')
      this.depts = depts
      this.loading = false
    },
    handleAddDepts(nodeData) {
      this.dialogVisible = true
      this.nodeData = nodeData
    },
    handleEditDepts(nodeData) {
      this.dialogVisible = true
      this.nodeData = nodeData
      // 之后的回显需求，需要调用请求，会拿到更详细的数据，而不用nodeData
      const addDeptRef = this.$refs.addDeptRef
      console.log(addDeptRef)
      // addDeptRef.getEmployeesSimple()
      addDeptRef.getDepartDetail(nodeData.id)
    }
  }
}
</script>

<style scoped lang='scss'>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
